package test.serverframe.armc.server.manager.dao.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;

import test.serverframe.armc.server.manager.controller.vo.InterfaceParamHistoryVo;
import test.serverframe.armc.server.manager.domain.InterfaceInvokingRecord;
import test.serverframe.armc.server.manager.domain.params.HistoryInterfaceParams;

/**
 * @author UaenaSA
 */
public interface InterfaceInvokingRecordMapper {

    @Delete({
        "delete from tb_ssgp_interface_invoking_record",
        "where interface_invoking_record_id = #{interfaceInvokingRecordId,jdbcType=VARCHAR}"
    })
    /**
     * 根据调度记录ID删除调度记录
     */
    int deleteByPrimaryKey(String interfaceInvokingRecordId);

    @Insert({
        "insert into tb_ssgp_interface_invoking_record (interface_invoking_record_id, invoke_task_record_id, ",
        "task_relate_inter_id, start_time, end_time, status, return_value, assert_value)",
        "values (#{interfaceInvokingRecordId,jdbcType=VARCHAR}, #{invokeTaskRecordId,jdbcType=VARCHAR}, ",
        "#{taskRelateInterId,jdbcType=VARCHAR}, #{startTime,jdbcType=TIMESTAMP}, #{endTime,jdbcType=TIMESTAMP}, ",
        "#{status,jdbcType=BIT}, #{returnValue,jdbcType=LONGVARCHAR}, #{assertValue,jdbcType=LONGVARCHAR})"
    })
    /**
     * 插入一条调度记录
     */
    int insert(InterfaceInvokingRecord record);

    @InsertProvider(type=InterfaceInvokingRecordSqlProvider.class, method="insertSelective")
    int insertSelective(InterfaceInvokingRecord record);

    @Select({
        "select",
        "interface_invoking_record_id, invoke_task_record_id, task_relate_inter_id, start_time, end_time, ",
        "status, return_value, assert_value",
        "from tb_ssgp_interface_invoking_record",
        "where interface_invoking_record_id = #{interfaceInvokingRecordId,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="interface_invoking_record_id", property="interfaceInvokingRecordId", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="invoke_task_record_id", property="invokeTaskRecordId", jdbcType=JdbcType.VARCHAR),
        @Result(column="task_relate_inter_id", property="taskRelateInterId", jdbcType=JdbcType.VARCHAR),
        @Result(column="start_time", property="startTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="end_time", property="endTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="status", property="status", jdbcType=JdbcType.BIT),
        @Result(column="return_value", property="returnValue", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="assert_value", property="assertValue", jdbcType=JdbcType.LONGVARCHAR)
    })
    InterfaceInvokingRecord selectByPrimaryKey(String interfaceInvokingRecordId);

    @UpdateProvider(type=InterfaceInvokingRecordSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(InterfaceInvokingRecord record);

    @Update({
        "update tb_ssgp_interface_invoking_record",
        "set invoke_task_record_id = #{invokeTaskRecordId,jdbcType=VARCHAR},",
          "task_relate_inter_id = #{taskRelateInterId,jdbcType=VARCHAR},",
          "start_time = #{startTime,jdbcType=TIMESTAMP},",
          "end_time = #{endTime,jdbcType=TIMESTAMP},",
          "status = #{status,jdbcType=BIT},",
          "return_value = #{returnValue,jdbcType=LONGVARCHAR}",
          "assert_value = #{assertValue,jdbcType=LONGVARCHAR}",
        "where interface_invoking_record_id = #{interfaceInvokingRecordId,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKeyWithBLOBs(InterfaceInvokingRecord record);

    @Update({
        "update tb_ssgp_interface_invoking_record",
        "set invoke_task_record_id = #{invokeTaskRecordId,jdbcType=VARCHAR},",
          "task_relate_inter_id = #{taskRelateInterId,jdbcType=VARCHAR},",
          "start_time = #{startTime,jdbcType=TIMESTAMP},",
          "end_time = #{endTime,jdbcType=TIMESTAMP},",
          "status = #{status,jdbcType=BIT}",
          "return_value = #{returnValue,jdbcType=LONGVARCHAR}",
          "assert_value = #{assertValue,jdbcType=LONGVARCHAR}",
        "where interface_invoking_record_id = #{interfaceInvokingRecordId,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(InterfaceInvokingRecord record);

    @Select({
    	"select status, return_value, assert_value, in_header, in_body, in_query, out_value",
    	"from tb_ssgp_interface_invoking_record as record, tb_ssgp_link_param as param",
    	"where record.task_relate_inter_id = (",
	    		"select task_enter_id",
	            "from tb_ssgp_task_enter_interface",
	            "where task_id = #{taskId,jdbcType=VARCHAR} and node_id = #{nodeId,jdbcType=VARCHAR}",
	        ")",
    		"and record.invoke_task_record_id = #{taskRecordId,jdbcType=VARCHAR}",
    		"and param.inter_relate_task_id = (",
	    		"select task_enter_id",
	            "from tb_ssgp_task_enter_interface",
	            "where task_id = #{taskId,jdbcType=VARCHAR} and node_id = #{nodeId,jdbcType=VARCHAR}",
	        ")"
    })
    @Results({
    	@Result(column = "in_header", property = "inHeader", jdbcType = JdbcType.VARCHAR),
        @Result(column = "in_body", property = "inBody", jdbcType = JdbcType.VARCHAR),
        @Result(column = "in_query", property = "inQuery", jdbcType = JdbcType.VARCHAR),
        @Result(column = "out_value", property = "outValue", jdbcType = JdbcType.VARCHAR),
        @Result(column = "status", property="status", jdbcType=JdbcType.BIT),
        @Result(column = "return_value", property="returnValue", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column = "assert_value", property="assertValue", jdbcType=JdbcType.LONGVARCHAR),
    })
    InterfaceParamHistoryVo findInterfaceParamsHistory(HistoryInterfaceParams params);

}